package com.example.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;

public class CheckBoxActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener{

    private CheckBox cbx1,cbx2,cbx3,cbx4,cbx5,cbx6,cbx7;
    private Button rdbtn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);

        cbx1 = findViewById(R.id.cb1);
        cbx2 = findViewById(R.id.cb2);
        cbx3 = findViewById(R.id.cb3);

        cbx4 = findViewById(R.id.cb4);
        cbx5 = findViewById(R.id.cb5);
        cbx6 = findViewById(R.id.cb6);
        cbx7 = findViewById(R.id.cb7);
        rdbtn2 = findViewById(R.id.rdidbtn2);

        cbx1.setOnCheckedChangeListener(this);
        cbx2.setOnCheckedChangeListener(this);
        cbx3.setOnCheckedChangeListener(this);
        cbx7.setOnCheckedChangeListener(this);
        rdbtn2.setOnClickListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if(b){
            if(compoundButton.getId() == R.id.cb7){
                cbx4.setChecked(true);
                cbx5.setChecked(true);
                cbx6.setChecked(true);
            }
            else
                Toast.makeText(this,compoundButton.getText().toString(),Toast.LENGTH_SHORT).show();
        }
        else {
            if(compoundButton.getId() == R.id.cb7){
                cbx4.setChecked(false);
                cbx5.setChecked(false);
                cbx6.setChecked(false);
            }
        }
    }

    @Override
    public void onClick(View view) {
        String choose = " ";
        if(cbx4.isChecked())choose += cbx4.getText().toString() + " ";
        if(cbx5.isChecked())choose += cbx5.getText().toString() + " ";
        if(cbx6.isChecked())choose += cbx6.getText().toString() + " ";
        Toast.makeText(this,choose,Toast.LENGTH_SHORT).show();
    }
}